package com.jushu.storbox.actions;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.jushu.storbox.R;
import com.jushu.storbox.callback.IUploadCallback;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.entities.FileInfo;
import com.jushu.storbox.manager.UploadTaskManager;
import com.jushu.storbox.task.tqueue.UploadTask;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.response.FileInfoResponseV2;

public class UploadAction extends BaseAction {
	
	private final static String TAG = UploadAction.class.getName();


	public void uploadFile(final Context context, final String ownerId, final String folderID, 
	        final List<String> filePaths, final Handler handler)
	    {
	        for (final String filePath : filePaths)
	        {
	            File file = new File(filePath);
	            if (!file.exists())
	            {
	                break;
	            }
	            final String name = file.getName();
	            LogUtil.i(TAG, "uploadFile... fileName:" + name);
	            IUploadCallback callback = new IUploadCallback()
	            {
	                
	                @Override
	                public void onSuccess(FileInfoResponseV2 successItem)
	                {
	                    FileInfo fileInformation = new FileInfo();
	                    fileInformation.valueOf(successItem);
	                    
	                    LogUtil.i(TAG, "uploadShareFile onSuccess...");
	                    Message message = new Message();
	                    message.what = MessageCode.UPLOAD_SUCCESS;
	                    message.obj = fileInformation;
	                    handler.sendMessage(message);
	                }
	                
	                @Override
	                public void onStart()
	                {
	                    LogUtil.i(TAG, "uploadShareFile start...");
	                }
	                
	                @Override
	                public void onProgress(int currentProgress, long currentSize, long maxSize)
	                {
	                }
	                
	                @Override
	                public void onFailure(Throwable t)
	                {
	                    LogUtil.e(TAG, "uploadShareFile... onFailure:" + t.getLocalizedMessage());
	                    if(t instanceof ClientException){
	                    	ClientException ex = (ClientException)t;
	                    	Message msg = new Message();
	                    	msg.what = ex.getStatusCode();
	                    	msg.obj = ex;
	                    	handler.sendMessage(msg);
	                    }else{
	                    	handler.sendEmptyMessage(MessageCode.UPLOAD_FAIL);
	                    }
	                }

					@Override
					public void onStop() {
					}

					@Override
					public void onCanceled() {
					}

					@Override
					public void onDettach() {
					}
	            };
	            UploadTask uploadTask = new UploadTask(context, ownerId, folderID,
	                filePath, null);
	            uploadTask.setCallback(callback);
	            boolean isAddSuccess = UploadTaskManager.addTask(uploadTask);
	            //
	            if (!isAddSuccess)
	            {// is Add Fail
	                LogUtil.e(TAG, "uploadShareFile... onFailure:"
	                    + context.getString(R.string.upload_failed_noneedupload_again));
	                if(UploadTaskManager.getAllTaskes().size() > 10)
	                handler.sendEmptyMessage(MessageCode.UPLOAD_FAIL);
	            }
	            
	        }
	    }
}
